package p_2023_03_09_StudentSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //创建存储学生对象的空间
        ArrayList<Student> list = new ArrayList<>();

        while (true) {
            menu();//展示主菜单
            System.out.println("请输入您的选择：");
            String select = sc.next();
            switch (select) {
                case "1" -> addStudent(list);//添加学生信息
                case "2" -> deleteStudent(list);//删除学生信息
                case "3" -> modifyStudent(list);//修改学生信息
                case "4" -> queryStudent(list);//查询学生信息
                case "5" -> displayStudent(list);//打印全部学生信息
                case "0" -> System.exit(0);//退出
                default -> System.out.println("没有该选项！");
            }
        }
    }

    //主菜单
    public static void menu() {
        System.out.println("---------------欢迎来到学生管理系统---------------");
        System.out.println("1. 添加学生");
        System.out.println("2. 删除学生");
        System.out.println("3. 修改学生");
        System.out.println("4. 查询学生");
        System.out.println("5. 展示学生");
        System.out.println("0. 退出");
        System.out.println("-------------------------------------------------");
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        Student stu = new Student();
        String uid;
        while (true) {
            System.out.println("请输入学生的id：");
            uid = sc.next();
            boolean flag = contain(list, uid);
            if (flag) {
                System.out.println("该学生已存在，请重新添加：");
            } else {
                stu.setId(uid);
                break;
            }
        }

        System.out.println("请输入学生的姓名：");
        String uname = sc.next();
        stu.setName(uname);

        System.out.println("请输入学生的年龄：");
        int uage = sc.nextInt();
        stu.setAge(uage);

        System.out.println("请输入学生的家庭住址：");
        String uaddress = sc.next();
        stu.setAddress(uaddress);

        list.add(stu);
        System.out.println("添加成功！");
    }

    public static boolean contain(ArrayList<Student> list, String id) {
        return index(list, id) >= 0;
    }

    public static int index(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生id：");
        String uid = sc.next();
        int uIndex = index(list, uid);
        if (uIndex >= 0) {
            list.remove(uIndex);
            System.out.println("删除成功！");
        } else {
            System.out.println("该学生不存在，删除失败！");
        }
    }

    //修改学生
    public static void modifyStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学生的id：");
        String uid = sc.next();
        int uIndex = index(list, uid);
        if (uIndex >= 0) {
            Student stu = list.get(uIndex);
            System.out.println("请输入学生的姓名：");
            String uname = sc.next();
            stu.setName(uname);

            System.out.println("请输入学生的年龄：");
            int uage = sc.nextInt();
            stu.setAge(uage);

            System.out.println("请输入学生的家庭住址：");
            String uaddress = sc.next();
            stu.setAddress(uaddress);
            System.out.println("修改成功！");
        } else {
            System.out.println("该学生不存在!");
        }
    }

    //查询学生
    public static void queryStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("按学生 id 查找：");
        System.out.println("请输入您要查询的学生id：");
        String uid = sc.next();
        int uIndex = index(list, uid);
        if (uIndex >= 0) {
            Student stu = list.get(uIndex);
            System.out.println("id\t\t" + "姓名\t\t" + "年龄\t\t" + "家庭住址");
            System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getAddress());
        } else {
            System.out.println("该学生信息不存在！");
        }
    }

    //打印全部学生信息
    public static void displayStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("There is Empty!\n");
            return;
        }
        System.out.println("===================学生信息如下======================");
        System.out.println("id\t\t" + "姓名\t\t" + "年龄\t\t" + "家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getAddress());
        }
        System.out.println("====================================================\n");
    }
}
